/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * ExternalFile Class:
 * This class is used to read/write the Inventory to an external file
 * @author Vale
 *
 */

public class ExternalFile {
    private final String INVENT_FILE = "inventory.txt";
    private final String ERR = "Error: ";
    private final String CHEESE = "Cheese Slice";
    private final String PICKLES = "Pickle Slices";
    private final String ONION = "Onion";
    private final String SAUCE = "Sauce";
    private final String BUNS = "Buns";
    private final String BEETROOT = "Beetroot Slices";
    private final String TOMATO = "Tomato Slices";
    private final String LETTUCE = "Shredded Lettuce";
    private final String CHICKEN_PATTY = "Chicken Patty";
    private final String BEEF_PATTY = "Beef Patty";
    private final String INV_FORM = "Incorrect File Format.";

    private final int INV_ITEMS = 10;
    /**
     * void readFile(InventoryList invList) Method:
     * Allows the user to read an inventory list from a text file, and store it in invList. Does this by comparing two arrays.
     * @param invList Inventory List for program
     *
     */
    public void readFile(InventoryList invList) {
        InventoryList invArr = new InventoryList();
        Lettuce lettuce = new Lettuce();
        invList.add(lettuce);
        BeefPatty beefPatty = new BeefPatty();
        invList.add(beefPatty);
        Beetroot beetroot = new Beetroot();
        invList.add(beetroot);
        Tomato tomato = new Tomato();
        invList.add(tomato);
        Onions onions = new Onions();
        invList.add(onions);
        Pickles pickles = new Pickles();
        invList.add(pickles);
        Buns buns = new Buns();
        invList.add(buns);
        ChickenPatty chickenPatty = new ChickenPatty();
        invList.add(chickenPatty);
        Sauce sauce = new Sauce();
        invList.add(sauce);
        Cheese cheese = new Cheese();
        invList.add(cheese);

        String[] strArr = new String[INV_ITEMS];
        strArr[0] = LETTUCE;
        strArr[1] = BEEF_PATTY;
        strArr[2] = BEETROOT;
        strArr[3] = TOMATO;
        strArr[4] = ONION;
        strArr[5] = PICKLES;
        strArr[6] = BUNS;
        strArr[7] = CHICKEN_PATTY;
        strArr[8] = SAUCE;
        strArr[9] = CHEESE;
        
        String line = null;
        try {
            BufferedReader fileIn = new BufferedReader(new FileReader(INVENT_FILE));
            for (int i=0; i<invList.size(); i++) {
                line = fileIn.readLine();
                for (int j=0; j<invList.size(); j++) {
                    if (line.equals(strArr[j])) {
                        invArr.get(j).setInvName(line);
                    } else {
                        System.out.println(INV_FORM);
                        return;
                    }
                }
                line = fileIn.readLine();
                int tempInvStock = Integer.parseInt(line);
                invArr.get(i).setInvStock(tempInvStock);
            }
            invList = invArr;
        } catch (IOException e) {
            System.out.println(ERR + e);
            return;
        } catch (NumberFormatException x) {
            System.out.println(INV_FORM);
            return;
        }


    }

    /**
     * void void writeFile(InventoryList invList) Method:
     * Allows the user to write an inventory list (invList) to a text file
     * @param invList Inventory List for program
     *
     */
    public void writeFile(InventoryList invList) {
        String line = null;
        try {
            FileWriter fileOut = new FileWriter(INVENT_FILE);
            for (int i = 0; i < invList.size(); i++) {
                line = invList.get(i).getInvName();
                fileOut.write(line);
                fileOut.write('\n');
                line = String.valueOf(invList.get(i).getInvStock());
                fileOut.write(line);
                fileOut.write('\n');
            }
            fileOut.close();
        } catch (IOException e) {
            System.out.println(ERR + e);

        }
    }
}
